package com.dexilaonian.activity;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.model.LatLng;
import com.dexilaonian.MyApplication;
import com.dexilaonian.utils.Constant;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/4/4.
 */

public class LocalUtils implements LocationSource, AMapLocationListener{

    private AMapLocationClient mLocationClient;
    private Context context;
    private OnLocationListener listener;
    private AMapLocationClientOption mLocationOption;


    public  LocalUtils(Context context, OnLocationListener listener) {
        //初始化定位
        mLocationClient = new AMapLocationClient(context);
        this.context = context;
        this.listener = listener;
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }

    /**
     * //启动定位
     */
    public void startLocation() {
        mLocationClient.startLocation();
    }

    /**
     * //停止定位
     */
    public void stopLocation() {
        mLocationClient.stopLocation();//停止定位
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见官方定位类型表
                aMapLocation.getLatitude();//获取纬度
                aMapLocation.getLongitude();//获取经度
                aMapLocation.getAccuracy();//获取精度信息
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(aMapLocation.getTime());
                df.format(date);//定位时间
                aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                aMapLocation.getCountry();//国家信息
                aMapLocation.getProvince();//省信息
                String city = aMapLocation.getCity();//城市信息
                aMapLocation.getDistrict();//城区信息
                aMapLocation.getStreet();//街道信息
                aMapLocation.getStreetNum();//街道门牌号信息
                aMapLocation.getCityCode();//城市编码
                aMapLocation.getAdCode();//地区编码
                MyApplication.city = aMapLocation.getCity();
                MyApplication.pervier = aMapLocation.getProvider();
                if (listener != null) {
                    listener.locationListener(aMapLocation.getCity());
                }
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            /*    Toast.makeText(context,"AmapError"+"location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo(), Toast.LENGTH_LONG).show();*/
                if (listener != null) {
                    listener.locationListener("定位失败");
                }
            }
        }
    }
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        listener = (OnLocationListener) onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        listener = null;
        //现在你要打开这个才能获取定位了
    }

    public interface OnLocationListener {
        void locationListener(String city);
    }
}
